package com.example.online_order.conrtoller;

import com.example.online_order.Eception.BussinessException;
import com.example.online_order.pojo.AdminAccount;
import com.example.online_order.pojo.Result;
import com.example.online_order.service.AdminAcountService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin")
@CrossOrigin
@Slf4j
public class AdminAccountController {
//    新增，删除，修改，查询（用户名查询  全表查询）
    @Autowired
    AdminAcountService  adminAcountService;

    @PostMapping("/add")
    public Result addadmin(@RequestBody AdminAccount account){
        Integer res = adminAcountService.addAdmin(account);
        if (res != null ){
            return new Result(account);
        }else{
           return  Result.fail("Admin account already",500);
        }
    }

    @PutMapping("/reset")
    public Result updateAdminPassword(@RequestBody AdminAccount account){
        Integer res = adminAcountService.updateAdminpassword(account);
        if (res == 1){
            return new Result("update password success");
        } else{
            return  Result.fail("update password failed",444);
        }
    }

    @DeleteMapping("/del/{id}")
    public Result delAdmin(@PathVariable("id") Integer id){
        Integer res = adminAcountService.deleteAdmin(id);
        if (res == 1){
            return new Result("delete success");
        }else{
            return  Result.fail("delete failed",444);
        }
    }

   @PutMapping("/update")
   public Result updateAdmin(@RequestBody AdminAccount account){
       Integer res = adminAcountService.updateAdmin(account);
       if (res == 1){
           return new Result("update success");
       } else{
           return  Result.fail("update failed",444);
       }
   }

   @GetMapping("/getAll")
    public Result getAllAdmin(){
       return new Result(adminAcountService.getAllAdmin());
   }

   @GetMapping("/getLike/{username}")
    public Result getByUsername(@PathVariable("username") String username){
        return  new Result(adminAcountService.findAdminLikeUsername(username));
   }


}
